/**
 * @file setting_dual_sim.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_DUAL_SIM != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * dualsim_create(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void dualsim_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;
    lv_watch_png_cache_all_free();

    lv_obj_t * obj = dualsim_create(NULL);
    LV_ASSERT_MEM(obj);
}

void dualsim_init(uint8_t simid)
{
    watch_modem_set_sim_id((MMI_MODEM_SIM_ID)simid);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_dualsim_obj_ext_t * dualsim_get_ext(void)
{
    lv_dualsim_obj_ext_t  * ext = NULL;
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_DUAL_SIM);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }
    return ext;
}

static void dualsim_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void dualsim_sim1_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_dualsim_obj_ext_t * ext = dualsim_get_ext();
    lv_checkbox_set_checked(ext->cb_sim1, true);
    lv_checkbox_set_checked(ext->cb_sim2, false);

    if(MMI_MODEM_SIM_1 != watch_modem_get_sim_id()) {
        MMI_MODEM_SIM_ID sim_id = MMI_MODEM_SIM_1;
        uint8_t nvdata = (uint8_t)sim_id;
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, 6, 1, &nvdata);

        //switch sim
        watch_modem_switch_to_sim(sim_id);
    }
}

static void dualsim_sim2_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_dualsim_obj_ext_t * ext = dualsim_get_ext();
    lv_checkbox_set_checked(ext->cb_sim1, false);
    lv_checkbox_set_checked(ext->cb_sim2, true);

    if(MMI_MODEM_SIM_2 != watch_modem_get_sim_id()) {
        MMI_MODEM_SIM_ID sim_id = MMI_MODEM_SIM_2;
        uint8_t nvdata = (uint8_t)sim_id;
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, 6, 1, &nvdata);

        //switch sim
        watch_modem_switch_to_sim(sim_id);
    }
}

static lv_obj_t * dualsim_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_DUAL_SIM;
        activity_ext.create = dualsim_create;
        activity_ext.prepare_destory = dualsim_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_dualsim_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_dualsim_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_DUAL_SIM);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * cb_sim1 = lv_checkbox_create(obj, NULL);
    lv_checkbox_set_text(cb_sim1, lv_lang_get_text(WATCH_TEXT_ID_SIM1));
    lv_obj_clean_style_list(cb_sim1, LV_CHECKBOX_PART_BULLET);
    lv_obj_add_style(cb_sim1, LV_CHECKBOX_PART_BULLET, &lv_watch_cb_styles);
    lv_obj_align(cb_sim1, obj, LV_ALIGN_CENTER, -10, -30);
    lv_obj_set_event_cb(cb_sim1, dualsim_sim1_btn_action);
    lv_watch_obj_add_element(cb_sim1);

    lv_obj_t * cb_sim2 = lv_checkbox_create(obj, cb_sim1);
    lv_checkbox_set_text(cb_sim2, lv_lang_get_text(WATCH_TEXT_ID_SIM2));
    lv_obj_align(cb_sim2, cb_sim1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_set_event_cb(cb_sim2, dualsim_sim2_btn_action);
    lv_watch_obj_add_element(cb_sim2);

    MMI_MODEM_SIM_ID sim_id = watch_modem_get_sim_id();
    if(MMI_MODEM_SIM_1 == sim_id) {
        lv_checkbox_set_checked(cb_sim1, true);
    } else if(MMI_MODEM_SIM_2 == sim_id) {
        lv_checkbox_set_checked(cb_sim2, true);
    }

    ext->cb_sim1 = cb_sim1;
    ext->cb_sim2 = cb_sim2;
    return obj;
}
#endif /*USE_LV_WATCH_DUAL_SIM*/
